package sg.edu.nus.iss.vmcs.customer;

import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionListener;

/*
 * @author nyi nyi than
 */
public class DrinkSelectionItem extends Panel
{
    private static final long serialVersionUID = 1L;
    private Button drinkButton;
    private Label priceLabel;
    private Label stockStatusLabel;
    private int stockStatus;
    public DrinkSelectionItem(String drinkName, String price,
	    int stockStatus, ActionListener listener)
    {
	this.setLayout(new GridLayout());
	setFont(new Font("Helvetica", Font.PLAIN, 12));

	this.stockStatus = stockStatus;
	
	drinkButton = new Button();
	if (stockStatus <= 0)
	    drinkButton.setEnabled(false);
	drinkButton.setLabel(drinkName);
	drinkButton.addActionListener(listener);

	add(drinkButton);

	priceLabel = new Label(price, Label.CENTER);
	priceLabel.setBackground(Color.black);
	priceLabel.setForeground(Color.white);

	add(priceLabel);

	stockStatusLabel = new Label(
		stockStatus <= 0 ? "Not in stock" : String.valueOf(stockStatus), Label.LEFT);
	stockStatusLabel.setBackground(Color.black);
	stockStatusLabel.setForeground(Color.white);

	add(stockStatusLabel);
    }

    public void setName(String name)
    {
	drinkButton.setLabel(name);
    }
    
    @Override
    public String getName()
    {
	return drinkButton.getLabel();
    }

    public void setPrice(String price)
    {
	priceLabel.setText(price);
    }
    
    public int getPrice(){
	String price = this.priceLabel.getText().trim();
	if(price.contains("$")){
	    price = price.replace("$", "").trim();
	    if(price.contains(".")){
		return (int) Double.parseDouble(price) * 100;
	    }
	    else return Integer.parseInt(price) * 100;
	}
	else if(price.contains("C")){
	    price = price.replace("C", "").trim();
	    return Integer.parseInt(price);
	}
	else return 0;
    }
    
    public void reduce(){
	this.stockStatus -= 1;
	if(this.stockStatus <= 0){
	    this.drinkButton.setEnabled(false);
	    stockStatusLabel.setText("Not in stock");
	}
	else
	    stockStatusLabel.setText(String.valueOf(this.stockStatus));
    }
    
    public void setItemState(boolean status)
    {	
	this.drinkButton.setEnabled(status && (this.stockStatus > 0));
    }
}
